//
//  WPCOrderView.m
//  Wallpaper.co
//
//  Created by zhou_yuepeng on 2017/9/20.
//  Copyright © 2017年 www.dahuatech.com. All rights reserved.
//

#import "WPCOrderView.h"
#import "WPCLayout.h"

@implementation WPCOrderModel
@end

@interface WPCOrderView()
@property (nonatomic, strong)UIImageView    *sortShadow;
@property (nonatomic, strong)UIView         *backView;
@property (nonatomic, strong)UIButton       *sortBtn;
@property (nonatomic, strong)UIImageView    *tagIcon;
@property (nonatomic, strong)UILabel        *tagLbl;
@property (nonatomic, strong)UILabel        *pageLbl;
@end

@implementation WPCOrderView
- (instancetype)init
{
    self = [super init];
    if (self) {
        _orderModel = [[WPCOrderModel alloc] init];
        
        _sortShadow = [[UIImageView alloc] init];
        [_sortShadow setImage:[UIImage imageNamed:@"order"]];
        [self addSubview:_sortShadow];
        
        _sortBtn = [[UIButton alloc] init];
        _sortBtn.tag = BTN_SORT;
        [self addSubview:_sortBtn];
        
        _backView = [[UIView alloc] init];
        [self addSubview:_backView];
        _backView.backgroundColor = [UIColor whiteColor];
        
        _tagIcon = [[UIImageView alloc] init];
        [_backView addSubview:_tagIcon];
        
        _tagLbl = [[UILabel alloc] init];
        [_backView addSubview:_tagLbl];
        _tagLbl.textAlignment = NSTextAlignmentLeft;
        
        _pageLbl = [[UILabel alloc] init];
        [_backView addSubview:_pageLbl];
        _pageLbl.textAlignment = NSTextAlignmentRight;
        
        [_orderModel addObserver:self forKeyPath:@"tagName" options:NSKeyValueObservingOptionNew context:nil];
        [_orderModel addObserver:self forKeyPath:@"currentPage" options:NSKeyValueObservingOptionNew context:nil];
        [_orderModel addObserver:self forKeyPath:@"totalPage" options:NSKeyValueObservingOptionNew context:nil];
        [_orderModel addObserver:self forKeyPath:@"byDate" options:NSKeyValueObservingOptionNew context:nil];
        
        _orderModel.byDate = YES;
        _orderModel.tagName = @"New";
    }
    
    return self;
}

- (void)dealloc
{
    /* 移除监听 */
    [_orderModel removeObserver:self forKeyPath:@"tagName"];
    [_orderModel removeObserver:self forKeyPath:@"currentPage"];
    [_orderModel removeObserver:self forKeyPath:@"totalPage"];
    [_orderModel removeObserver:self forKeyPath:@"byDate"];
}

- (void)layoutSubviews
{
    [self layoutWithMasonry];
}

- (void)layoutWithMasonry
{
    [_sortShadow makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(self);
        make.size.equalTo(CGSizeMake(self.bounds.size.height, self.bounds.size.height));
    }];
    
    [_sortBtn makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_sortShadow);
        make.size.equalTo(_sortShadow);
    }];
    
    [_backView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_sortShadow.right);
        make.top.and.right.and.bottom.equalTo(self);
    }];
    
    [_tagIcon makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.equalTo(_backView);
        make.size.equalTo(_sortShadow);
    }];
    
    [_tagLbl makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_tagIcon.right);
        make.top.and.height.equalTo(_backView);
        make.width.equalTo(_backView).multipliedBy(0.4);
    }];
    
    [_pageLbl makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_tagLbl.right);
        make.right.equalTo(_backView).offset(-10);
        make.top.and.height.equalTo(_backView);
    }];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if (object != _orderModel) {
        return;
    }
    
    if ([keyPath isEqualToString:@"tagName"]) {
        UIColor *textColor = nil;
        UIImage *icon = nil;
        NSString *tagName = _orderModel.tagName;
        if ([_orderModel.tagName isEqualToString:@"New"]) {
            icon = [UIImage imageNamed:@"newsmall"];
            textColor = [UIColor colorWithRed:248.0/255.0 green:198.0/255.0 blue:53.0/255.0 alpha:1];
        }
        else if ([_orderModel.tagName isEqualToString:@"Best"]) {
            icon = [UIImage imageNamed:@"bestsmall"];
            textColor = [UIColor colorWithRed:9.0/255.0 green:209.0/255.0 blue:197.0/255.0 alpha:1];
        }
        else if ([_orderModel.tagName isEqualToString:@"Favorites"]) {
            icon = [UIImage imageNamed:@"lovesmall"];
            textColor = [UIColor colorWithRed:253.0/255.0 green:69.0/255.0 blue:102.0/255.0 alpha:1];
        }
        else {
            icon = [UIImage imageNamed:@"tag"];
            textColor = [UIColor blackColor];
        }
        
        [_tagLbl setText:tagName];
        [_tagLbl setTextColor:textColor];
        [_tagIcon setImage:icon];
    }
    else if ([keyPath isEqualToString:@"currentPage"] || [keyPath isEqualToString:@"totalPage"]) {
        NSString *text = [NSString stringWithFormat:@"%ld / %ld", (long)_orderModel.currentPage, (long)_orderModel.totalPage];
        [_pageLbl setText:text];
    }
    else if ([keyPath isEqualToString:@"byDate"]) {
        [_sortBtn setImage:[UIImage imageNamed:_orderModel.byDate ? @"bydate" : @"random"] forState:UIControlStateNormal];
    }
}

@end
